package com.ecy.quadratic.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.SectionAdapter;
import com.ecy.quadratic.beans.ComicDetail;
import com.ecy.quadratic.beans.Sections;
import com.ecy.quadratic.beans.support.ComicSectionsEvent;
import com.ecy.quadratic.component.DaggerComicDetailComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.activity.ComicReadActivity;
import com.ecy.quadratic.ui.contract.ComicSectionsContract;
import com.ecy.quadratic.ui.presenter.ComicSectionsPresenter;
import com.ecy.quadratic.widget.ScrollGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class ComicSectionsFragment extends BaseFragment implements ComicSectionsContract.View {

    private static final String COMIC_ID = "comic_id";
    private static final String SECTIONS = "sections";
    @Bind(R.id.comicdetail_sections_gridview)
    ScrollGridView mGridView;
    @Inject
    ComicSectionsPresenter mSectionsPresenter;
    ComicDetail mComicDetail;
    private SectionAdapter mSectionAdapter;
    private Sections mSections = new Sections();

    public static ComicSectionsFragment newInstance(String comicId) {
        ComicSectionsFragment fragment = new ComicSectionsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(COMIC_ID, comicId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_comic_sections;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerComicDetailComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mSectionsPresenter != null) {
            mSectionsPresenter.attachView(this);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mSectionsPresenter != null) {
            mSectionsPresenter.detachView();
        }
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        mSectionAdapter = new SectionAdapter(getActivity(), mSections);
    }

    @Override
    public void configViews() {
        mGridView.setFocusable(false);
        mGridView.setAdapter(mSectionAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ComicReadActivity.startActivity(getActivity(),
                        mComicDetail.getId(), mComicDetail.getTitle(), mComicDetail.getImg(),
                        mComicDetail.getSections(), mSections.get(position));
            }
        });

    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void refreshSectionsUI(ComicSectionsEvent event) {
        mSections = event.comicDetail.getSections();
        mSectionAdapter.updateData(mSections);
        mComicDetail = event.comicDetail;
    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void updateReadingSection(String chapter) {

    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }
}
